package servidor;

import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import maquina.LocalProperties;

public class FileServerSocket extends Thread {
	  public void run(){
		    try {  
	         ServerSocket ss = new ServerSocket(LocalProperties.fileTransferExternalPort);  
	         while(true) {  
	           System.out.println("FileServer Activated");  
	           Socket socket = ss.accept();  
	           ObjectInputStream in = new ObjectInputStream(socket.getInputStream());  
	           String fileName = in.readUTF();  
	           FileOutputStream fos = new FileOutputStream("files//"+fileName);  
	           byte[] buf = new byte[4096];  
	           while(true) {  
	             int len = in.read(buf);  
	             if(len == -1) break;  
	               
	             fos.write(buf, 0, len);  
	           }  
	           fos.flush();  
	           fos.close();  
	         }  
	       } catch (Exception ex) {  
	         ex.printStackTrace();  
	       }  
	  }
}